<?php

namespace App\Http\Controllers;

use App\Chest;
use App\Fault;
use App\FaultType;
use App\Grip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DeviceController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        $groups = $user->group()->get();


        return view('device.index', compact('groups'));
    }
    

    public function repairForm(Request $request,$id=0)
    {
        $user = Auth::user();
        $chestNumber = '869300038720094';//$request->get('chest_number');
        $chest = Chest::where('chest_number', $chestNumber)->first();
        if (!$chest) {
            return $this->error('柜子编号错误');
        }

        $faultTypes = FaultType::get();
        if(!$id){
            $group = $user->group()->where(['device_type'=>'App\Chest','device_id'=>$chest['id']])->first();
        }else{
            $group = $user->group()->find($id);
        }
        //$chestId = $group->getSlug('chest');

        $grips = Grip::where(['chest_id' => $chest['id']])->get();
        $slug= explode('_',$group['slug']);
        $count = $grips->count();
        return view('device.repair', compact('user', 'faultTypes', 'group', 'grips', 'count','chest','slug'));
    }

    public function repair(Request $request, $id=0)
    {

        $user = Auth::user();

        $group = $user->group()->find($id);

        $gripId = $group->getSlug('grip');
        $chestId = $group->getSlug('chest');
        $fault = Fault::where('grip_id', $gripId)->first();

        if ($fault) {
            return $this->message('您已经提交过报修，不可以重复提交哟', route('user.index'));
        }

        $data = [
            'fault_type' => $request->get('fault_type'),
            'update_grip_id' => $request->get('update_grip_id'),
            'handle_way' => $request->get('handle_way'),
            'fault_desc' => $request->get('fault_desc'),
            'chest_id' => $chestId,
            'grip_id' => $gripId,
            'user_id' => $user->id
        ];


        $fault = Fault::create($data);

        if ($fault) {
            $group->repair();
            return $this->message('提交成功', route('user.device'));
        } else {
            return $this->message('报修失败', route('user.index'));
        }
    }

    public function repairList()
    {
        $user = Auth::user();
        return view('user.repair', compact('user'));
    }

    public function repairRecords()
    {
        //维修设备任务记录 to do
        $user = Auth::user();
        $Chests = $user->chest()->pluck('id');
        $records = Fault::whereIn('chest_id', $Chests)->where(['state' => 1])->get();
        return view('device.repairRecords', compact('user', 'records'));
    }

    public function allEquipment()
    {
        //vendor全部设备 to do
        $user = Auth::user();
        if ($user->isAgent()) {
            $Chests = Chest::where(['agent_id' => $user->id])->get();
        } else if ($user->isInvest()) {
            $Chests = Chest::where(['invesor_id' => $user->id])->get();
        } else {
            $Chests = Chest::where(['parteny_id' => $user->id])->get();
        }
        return view('device.allEquipment', compact('user', 'Chests'));
    }

    public function waitingForRepair()
    {
        //等待维修设备 to do
        $user = Auth::user();
        $Chests = $user->chest()->pluck('id');
        $faults = Fault::whereIn('chest_id', $Chests)->where(['state' => 0])->get();
        return view('device.waitingForRepair', compact('user', 'faults'));
    }

    public function chestGrip(Request $request)
    {
        $number = $request->get('number');
        //利用number 拿到柜子的id  然后通过id拿到格子
        $chestId = Chest::where(['chest_number' => $number])->first();
        $grips = Grip::where(['chest_id' => $chestId['id']])->get();
        return $this->jsonSuccess('成功', $grips);
    }


}
